//#include "StdAfx.h"
#include "JuliaNervus.h"


JuliaNervus::JuliaNervus(void):m_sInputSignalArray(NULL),m_sOutputSignalArray(NULL),m_nSignalCount(0)
{
	//m_cValue = Complex( M_E*cos(DEFAULT_ANGLE_NERVUS), M_E*sin(DEFAULT_ANGLE_NERVUS) );
}


JuliaNervus::~JuliaNervus(void)
{
	if( m_sInputSignalArray )
		delete []m_sInputSignalArray;
	if( m_sOutputSignalArray )
		delete []m_sOutputSignalArray;
}

void JuliaNervus::InitSignal(int nCount, Complex def)
{
	m_nSignalCount = nCount;
	m_sInputSignalArray = new JuliaNervusSignalLink[nCount];
	m_sOutputSignalArray = new JuliaNervusSignalLink[nCount];
	m_cValue = def;
}

void JuliaNervus::SetOutSignalParam( int nIdx, JuliaNervus* pTo, int exp )
{
	m_sOutputSignalArray[nIdx].m_pToNervus = pTo;
	m_sOutputSignalArray[nIdx].m_nExpire = exp;
	pTo->SetInSignalParam( nIdx, exp );
}

void JuliaNervus::SetInSignalParam( int nIdx, int exp )
{
	m_sInputSignalArray[nIdx].m_nExpire = exp;
}

void JuliaNervus::SetInputSignal( int nIdx, Complex input )
{
	if( input.real !=0 || input.image !=0 )
	{
		m_sInputSignalArray[nIdx].m_cValue = input;
		m_sInputSignalArray[nIdx].m_bActive = true;
	}
	else
		m_sInputSignalArray[nIdx].m_bActive = false;
}

bool JuliaNervus::ProcessSignal( void )
{
	bool bRet = false;

	for( int i=0; i<m_nSignalCount; i++ )
		if( m_sInputSignalArray[i].m_bActive && m_sInputSignalArray[i].m_nExpire>0 )
		{
			//if( i!=0 )
			//	printf("->%d\n",i);
			m_cValue = m_cValue.CalcNewValue( m_sInputSignalArray[i].m_cValue );
			bRet = true;

			for( int j=0; j<m_nSignalCount; j++ )
			{
				m_sOutputSignalArray[j].m_bActive = true;
				m_sOutputSignalArray[j].m_cValue = m_cValue;
			}

			m_sInputSignalArray[i].m_bActive = false;
			m_sInputSignalArray[i].m_nExpire--;
		}

	return bRet;
}

void JuliaNervus::TransmitSignal( void )
{
	for( int i=0; i<m_nSignalCount; i++ )
		if( m_sOutputSignalArray[i].m_bActive )
		{
			m_sOutputSignalArray[i].m_pToNervus->SetInputSignal( i, m_sOutputSignalArray[i].m_cValue );
			m_sOutputSignalArray[i].m_bActive = false;
		}
}

Complex JuliaNervus::GetOutputSignalValue( void )
{
	if( m_sOutputSignalArray[0].m_bActive )
		return m_sOutputSignalArray[0].m_cValue;
	else
		return Complex(0,0);
}
